package com.wise.site.more;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.wise.site.R;
import com.wise.site.main.MainIndexActivity;
import com.wise.site.main.MoreActivity;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.PushEntryData;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.FileCache;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings.Secure;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

public class MorePush extends Activity {
	private String androidId;
	private PushAdapter adapter;
	private ProgressDialog mProgressDialog;
	private Handler handle = new Handler(){
		public void handleMessage(android.os.Message msg) {
			adapter.notifyDataSetChanged();
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_push);
		androidId = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
		ListView listView = (ListView)findViewById(R.id.list);
		((TextView)findViewById(R.id.activity_title)).setText("通知消息"); 
		((ImageButton)findViewById(R.id.title_back)).setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				startActivity(new Intent(getApplicationContext(), MainIndexActivity.class));
				finish();
			}
		});
		adapter = new PushAdapter();
		listView.setAdapter(adapter);
		
		listView.setBackgroundColor(0xffffffff);
	}
	
	public class PushAdapter extends BaseAdapter{
		private ArrayList<PushEntryData> list;
		private OnClickListener l = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				final PushEntryData e = (PushEntryData) v.getTag();
				SoapAction<Integer> sAction = new SoapAction<Integer>(
						ACTION_TYPE.ACTION_COMMON, "msg_clickurl") {
							@Override
							protected Integer parseJson(String response)
									throws Exception {
								JSONObject json = new JSONObject(response);
								int flag = 0;
								if(!json.isNull("flag")){
									flag = json.getInt("flag");
								}
								return flag;
							}
					
				};
				sAction.addJsonParam("AccountID", Constanct.INDUSTRY_ID + "");
				sAction.addJsonParam("PhoneType", 1);
				sAction.addJsonParam("msgid", e.id);

				ProtocolManager.getProtocolManager().submitAction(sAction);
				
				try{
					if(e.url != null){
						startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(e.url)));
					}
				}catch (Exception e1) {
				}
			}
		};
		
		public PushAdapter(){
			list = new ArrayList<PushEntryData>();
			SoapAction<Integer> sAction = new SoapAction<Integer>(
					ACTION_TYPE.ACTION_COMMON, "msg_getmsg") {
				@Override
				protected Integer parseJson(String response)
						throws Exception {
					JSONObject json = new JSONObject(response);
					int flag = 0;
					if(!json.isNull("flag")){
						flag = json.getInt("flag");
						JSONArray array = new JSONArray(json.getString("msg"));
						if(flag == 1 && array.length() > 0) {
							for(int i = 0; i < array.length(); i++){
								json = array.getJSONObject(i);
								PushEntryData e = new PushEntryData();
								e.id = json.getInt("id");
								e.content = json.getString("content");
								if(!json.isNull("url")){
									e.url = json.getString("url");
								}
								e.date = json.getString("time");
								list.add(e);
							}
							
							FileCache.getInstance().put("push", list, true);
							handle.sendEmptyMessage(1);
						}
					}
					return flag;
				}
			};
			sAction.addJsonParam("AccountID", Constanct.INDUSTRY_ID + "");
			sAction.addJsonParam("UUID", androidId);
			sAction.addJsonParam("PhoneType", 1);
			sAction.addJsonParam("IP", "");
			sAction.setActionListener(new ActionListener<Integer>() {

				@Override
				public void onSucceed(Integer result) {
					if(mProgressDialog.isShowing()){
						mProgressDialog.dismiss();
					}
				}

				@Override
				public void onError(int resultCode) {
					try{
						list = (ArrayList<PushEntryData>) FileCache.getInstance().get("push");
						handle.sendEmptyMessage(1);
					}catch (Exception e) {
					}
					if(mProgressDialog.isShowing()){
						mProgressDialog.dismiss();
					}
				}
			});

			ProtocolManager.getProtocolManager().submitAction(sAction);
			showProgress();
		}
		
		
		
		protected void showProgress() {
			if(mProgressDialog == null){
				mProgressDialog = ProgressDialog.show(MorePush.this, null, getString(R.string.loading));
			}else if(!mProgressDialog.isShowing()){
				mProgressDialog.show();
			}
			new Handler().postDelayed(new Runnable() {
				
				@Override
				public void run() {
					try{
						mProgressDialog.dismiss();
					}catch (Exception e) {
					}
				}
			}, 16000);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if(list == null) {
				return 0;
			}
			
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int i, View v, ViewGroup arg2) {
			v = getLayoutInflater().inflate(R.layout.list_push_item, null);
			PushEntryData e = list.get(i);
			((TextView)v.findViewById(R.id.title)).setText(e.content);
			((TextView)v.findViewById(R.id.snipping)).setText(e.date);
			v.setTag(e);
			if(e.url != null && e.url.length() > 5){
				v.setOnClickListener(l);
			} else {
				v.findViewById(R.id.right_arrow).setVisibility(View.INVISIBLE);
			}
			
			return v;
		}
		
	}
	
	
}
